using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango;
using Pixysoft.Framework.Noebe;
using Pixysoft.SQL;
using Pixysoft.Weblications.Sango.Schemas;
using Pixysoft.Weblications.Sango.Entities;
using Pixysoft.Framework.Verifications;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Tools;

namespace Pixysoft.Weblications.Sango.Dao
{
    public class StrategyDao
    {
        public static Usr_Strategy createStrategy(Usr_Profile profile, Entities.Cti_Strategy orm)
        {
            Usr_Strategy usrorm = Entities.Local.Usr_Strategy;
            usrorm.Primarycode = (PkHelper.PrimaryCode());
            usrorm.Usercode = (profile.Usercode);
            usrorm.Nickname = (profile.Nickname);
            usrorm.Strategytypecode = (orm.Strategytypecode);
            usrorm.Strategyname = (orm.Strategyname);
            usrorm.Strategylevel = (PlayerFormular.default_startlevel_strategy);
            usrorm.Strategyvalue = (orm.Strategyvalue * usrorm.Strategylevel);
            return usrorm;
        }

        protected static Usr_Strategy tryGetStrategy(String userCode, int strategytype)
        {
            IEntityNoebeCommand command = SettingHelper.GetNoebe().EntityNoebeCommand;
            command.SQL = (//
                    SqlCreator.SelectAll().From(Schemas.LOCAL.USR_STRATEGY)//
                            .Where(Schemas.LOCAL.USR_STRATEGY.USERCODE).Equal("USERCODE", true)//
                            .And(Schemas.LOCAL.USR_STRATEGY.STRATEGYTYPECODE).Equal("TYPECODE", true).GetSQL());
            command.Parameters.Add("USERCODE", userCode);
            command.Parameters.Add("TYPECODE", strategytype);

            List<Usr_Strategy> strategies = command.ExecuteReader<Usr_Strategy>();

            if (strategies.Count == 0)
                return null;

            return strategies[0];
        }


        internal static List<Usr_Strategy> getStrategies(string userCode)
        {
            IEntityNoebeCommand command = SettingHelper.GetNoebe().EntityNoebeCommand;
            command.SQL = (//
                    SqlCreator.SelectAll().From(Schemas.LOCAL.USR_STRATEGY)//
                            .Where(Schemas.LOCAL.USR_STRATEGY.USERCODE).Equal("USERCODE", true).GetSQL());
            command.Parameters.Add("USERCODE", userCode);

            List<Usr_Strategy> strategies = command.ExecuteReader<Usr_Strategy>();

            return strategies;
        }


        public static Usr_Strategy getStrategy(String userCode, long strategytype)
        {
            Usr_Strategy strategy = tryGetStrategy(userCode, strategytype);

            if (strategy == null)
                throw Exceptions.VerificationFailedException(userCode, strategytype);

            return strategy;
        }

        private static Usr_Strategy tryGetStrategy(String userCode, long strategytype)
        {
            IEntityNoebeCommand command = SettingHelper.GetNoebe().EntityNoebeCommand;
            command.SQL = (//
                    SqlCreator.SelectAll().From(Schemas.LOCAL.USR_STRATEGY)//
                            .Where(Schemas.LOCAL.USR_STRATEGY.USERCODE).Equal("USERCODE", true)//
                            .And(Schemas.LOCAL.USR_STRATEGY.STRATEGYTYPECODE).Equal("TYPECODE", true).GetSQL());
            command.Parameters.Add("USERCODE", userCode);
            command.Parameters.Add("TYPECODE", strategytype);

            List<Usr_Strategy> strategies = command.ExecuteReader<Usr_Strategy>();

            if (strategies.Count == 0)
                return null;

            return strategies[0];
        }


        internal static Usr_Strategy trySearchStrategy(List<Entities.Usr_Strategy> orms, long typecode)
        {
            foreach (Usr_Strategy orm in orms)
            {
                if (orm.Strategytypecode == typecode)
                    return orm;
            }

            return null;
        }



        internal static Usr_Strategy searchStrategy(List<Usr_Strategy> strategies, long strategytypecode)
        {
            foreach (Entities.Usr_Strategy orm in strategies)
            {
                if (orm.Strategytypecode == strategytypecode)
                    return orm;
            }

            throw Exceptions.VerificationFailedException(strategies, strategytypecode);
        }
    }
}